﻿/****************************** Module Header ******************************\
 * Module Name:  ExMethodForListControl.cs
 * Module Version:  1.0.0 (2012-9-14)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to extension Form instance's method.  It supplies 
 * public static extension methods to load data or get othor control.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Windows.Forms;

namespace UCF.Core.Forms
{
    /// <summary>
    /// ListBox扩展方法
    /// </summary>
    public static class ExMethodForListControl
    {
        /// <summary>
        /// 设置数据源
        /// </summary>
        /// <param name="c"></param>
        /// <param name="source"></param>
        public static void SetDataSource(this ListControl c, DataTable source, string displayName = "Name", string valueName = "ID")
        {
            c.DataSource = source;
            c.DisplayMember = displayName;
            c.ValueMember = valueName;
        }

        public static IList<T> GetSelectedValues<T>(this ListBox c,int cellIndex=0)
        {
            IList<T> lst = new List<T>();
            T v;
            foreach (object i in c.SelectedItems)
            {
                if (i.GetType() == typeof(DataRowView))
                {
                    v = (T)((DataRowView)i)[cellIndex];
                }
                else
                {
                    v = (T)i;
                }
                lst.Add(v);
            }
            return lst;
        }
    }
}
